<template>
	<view class="default-background">
		<view class="submit-feedback-page">
			<view class="submit-item-group">
				<view class="close-btn">
					<uni-icons type="closeempty" size="30" @click="back"></uni-icons>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="请填写标题" v-model="feedback.title" maxlength="50"/>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<textarea class="item-input" placeholder="请填写内容" v-model="feedback.content" maxlength="500"></textarea>
					</view>
				</view>
				<view class="item-group" style="border-bottom: none;">
					<view class="item-bar">上传图片</view>
					<uni-file-picker :value="filePathsList" :auto-upload="false" file-mediatype="image" mode="grid" class="imgs"
								file-extname="png,jpg" :limit="3" @select="handleSelect" @delete="handleDelete" @success="success" />
				</view>
			</view>
		</view>
		<view class="btn-group">
			<view class="confirmBtn button" @click="publish">提交</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				filePathsList: [],
				feedback: {
					title: "",
					content: "",
					img: ""
				},
				imgList: [],
				imgUrlList: []
			};
		},
		methods: {
			handleSelect(res) {
				this.imgList.push(res.tempFilePaths[0]);
			},
			back() {
				uni.navigateBack()
			},
		  publish() {
				if (!this.checkFeedbackInfo()) {
					return;
				}
				if (this.imgList.length > 0) {
					this.$request.uploadCurrentImage(this.imgList).then(res=>{
						this.feedback.img = JSON.stringify(res);
						this.$request.request({
							url: "/feedback/submit",
							method: "POST",
							data: this.feedback,
						}).then(res=>{
							console.log(res);
							if (res.code == 401) {
								uni.showModal({
									title: res.message + "，是否跳转登录界面",
									cancelText:'否',
									confirmText:'是',
									success(res){
									  if(res.cancel){
									    
									  }else if(res.confirm){
									    uni.navigateTo({
									    	url: "../user/login"
									    })
										}
									}
								})
							} else if (res.code == 200) {
								uni.showToast({
									title: "提交成功，跳转中...",
									icon: "none",
									duration: 2000
								});
								setTimeout(function() {
								  uni.navigateBack()
								}, 1500);
							}
						});
					})
				} else {
					this.$request.request({
						url: "/feedback/submit",
						method: "POST",
						data: this.feedback,
					}).then(res=>{
						console.log(res);
						if (res.code == 401) {
							uni.showModal({
								title: res.message + "，是否跳转登录界面",
								cancelText:'否',
								confirmText:'是',
								success(res){
								  if(res.cancel){
								    
								  }else if(res.confirm){
								    uni.navigateTo({
								    	url: "../user/login"
								    })
									}
								}
							})
						} else if (res.code == 200) {
							uni.showToast({
								title: "提交成功，跳转中...",
								icon: "none",
								duration: 2000
							});
							setTimeout(function() {
							  uni.navigateBack()
							}, 1500);
						}
					});
				}
				
			},
			checkFeedbackInfo() {
				
				this.feedback.title = this.feedback.title.trim();
				this.feedback.content = this.feedback.content.trim();
				
				// 标题
				if (this.feedback.title == "") {
					uni.showToast({
						title: "请填写标题",
						icon: "none"
					})
					return false;
				}
				if (this.feedback.title.length > 50) {
					uni.showToast({
						title: "标题过长",
						icon: "none"
					})
					return false;
				}
				
				console.log(this.feedback.content.length);
				// 描述
				if (this.feedback.content == "") {
					uni.showToast({
						title: "请填写描述",
						icon: "none"
					})
					return false;
				}
				if (this.feedback.content.length > 500) {
					uni.showToast({
						title: "描述内容过长",
						icon: "none"
					}, 3000)
					return false; 
				}
				
				return true;
			},
			// 将当前选择的所有图片上传到后台
			uploadCurrentImage() {
				for (let i=0; i<this.imgList.length; i++) {
					this.$request.fileUploadImage(this.imgList[i]).then(fileRes => {
						fileRes = JSON.parse(fileRes)
						this.imgUrlList.push(fileRes.data.urls[0].url);
					});
				}
				this.house.img = JSON.stringify(this.imgUrlList);
			},
			checkNum(e) {
			  //正则表达试
			  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null
			  //重新赋值给input
			  this.$nextTick(() => {
					this.house.price= e.target.value
					})
			},
			handleDelete(err) { // 删除图片
			    const num = this.filePathsList.findIndex(v => v.url === err.tempFilePath);
			    this.filePathsList.splice(num, 1);
					this.imgList = [];
					this.imgUrlList = [];
			},
		}
	}
</script>

<style lang="scss">
	.submit-feedback-page {
		
		.submit-item-group {
			background-color: #FFFFFF;
			margin: 10rpx 10rpx;
			padding: 30rpx 20rpx;
			border-radius: 20rpx;
			
			.item-group {
				border-bottom: #f2f1f6 1rpx solid;
				
				.item-input-group {
					padding: 20rpx 0;
					
					.item-input {
						font-size: 16px;
						color: #000000;
						text-align: justify;
					}
				}
				
				.item-bar {
					font-size: 18px;
					margin-top: 10rpx;
				}
				
				.imgs {
					margin-top: 10rpx;
				}
			}
			
		}
		
	}

</style>